<?php
/**
 * Created by PhpStorm.
 * User: ycp
 * Date: 2019/5/23
 * Time: 22:23
 */

namespace app\api\controller;


use app\common\func\Func;
use library\Controller;

class User extends ApiCommon
{
    protected $table = 'store_member';
    protected $infoTable = 'lite_user_info';

    public function index()
    {
        $user = db('StoreMember')->where(['openid' => $this->openid])->find();
        $user['couponCount'] = db('lite_coupons_record')->where(['open_id' => $this->openid, 'status' => 1,'is_deleted' => 0])->count();
        $user['nickname'] = urldecode($user['nickname']);
        $this->success('信息获取成功！', $user);
    }

    public function saveBase()
    {
        $data = $this->request->param();
        $updateData = [
            'headimg' => $data['avatarUrl'],
            'nickname' => urlencode($data['nickName']),
        ];
        $res = db('StoreMember')->where(['openid' => $this->openid])->update($updateData);
        $this->success('信息更新成功！', $res);
    }

    public function editInfo()
    {
        $data = $this->request->param();
        if(empty($this->openid)){
            $this->error('请先登录！');
        }
        $has = db($this->infoTable)->where(['lite_user_id' => $this->openid])->find();
        $insertData = [
            'real_name' => $data['data_0'],
            'country' => $data['data_1'],
            'id_card' => $data['data_2'],
            'mobile' => $data['data_3'],
            'birthday' => $data['data_4'],
            'sex' => $data['data_5'],
            'blood_type' => $data['data_6']['result']['steps2'],
            'city_label' => $data['city']['label'],
            'province' => $data['city']['value'][0],
            'city' => $data['city']['value'][1],
            'area' => $data['city']['value'][2],
            'address' => $data['data_8'],
            'postcode' => $data['data_9'],
            'email' => $data['data_10'],
            'clothing_size' => $data['data_11']['result']['steps2'],
            'passport_number' => $data['data_12'],
            'passport_surname' => $data['data_13'],
            'passport_name' => $data['data_14'],
            'passport_issuance_time' => $data['data_15'],
            'passport_end_time' => $data['data_16'],
            'is_doing' => $data['data_17'],
            'malathion_finish_time' => $data['data_18'],
            'emergency_contact' => $data['data_19'],
            'emergency_contact_phone' => $data['data_20'],
//            'health_certificate_pic' => $data['data_11'],
//            'finish_activity_pic' => $data['data_11'],
        ];
        $insertData['lite_user_id'] = $this->openid;
        if(!$has){
            $result = db($this->infoTable)->insert($insertData);
            if($result){
                $this->success('信息保存成功！', $result);
            }
        }else{
            $where = ['lite_user_id' => $this->openid];
            $result = db($this->infoTable)->where($where)->update($insertData);
            if($result){
                $this->success('信息保存成功！', $result);
            }
        }

    }

    public function editInfoPic()
    {
        $data = $this->request->param();
        if(empty($this->openid)){
            $this->error('请先登录！');
        }
        $where = ['lite_user_id' => $this->openid];
        $has = db($this->infoTable)->where($where)->find();
        $insertData = [
            'health_certificate_pic' => $data['health_certificate_pic'],
            'finish_activity_pic' => $data['finish_activity_pic'],
        ];
        $insertData['lite_user_id'] = $this->openid;
        if(!$has){
            $result = db($this->infoTable)->insert($insertData);
            if($result){
                $this->success('图片保存成功！', $result);
            }
        }else{
            $result = db($this->infoTable)->where($where)->update($insertData);
            if($result){
                $this->success('图片保存成功！', $result);
            }
        }

    }

    public function getInfo()
    {
        if(empty($this->openid)){
            $this->error('请先登录！');
        }
        $where = ['lite_user_id' => $this->openid];
        $has = db($this->infoTable)->where($where)->find();
        $bloodType = ["A" => 0, "B" => 1, "AB" => 2,"O" => 3];
        $clothing_size = ["XS" => 0, "S" => 1,"M" => 2,"L" => 3,"XL" => 4,"XXL" => 5];
        $has['blood_type'] = $bloodType[$has['blood_type']];
        $has['clothing_size'] = $clothing_size[$has['clothing_size']];
        $this->success('获取成功！', $has);

    }
}